package com.lagou.edu.factory;

import com.lagou.edu.pojo.Company;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author 赵乾坤
 * @since 2021-12-05 15:56
 * 公司工厂对象演示
 *Bean创建的三种⽅式中的静态⽅法和实例化⽅法和FactoryBean作⽤类似，FactoryBean使⽤较多，尤
 * 其在Spring框架⼀些组件中会使⽤，还有其他框架和Spring框架整合时使⽤
 */
public class CompanyFactoryBean implements FactoryBean<Company>{

    /**
     * 公司名称,地址,规模
     */
    private String companyInfo;

    public void setCompanyInfo(String companyInfo) {
        this.companyInfo = companyInfo;
    }

    @Override
    public Company getObject() throws Exception {
        // 模拟创建复杂对象Company
        Company company = new Company();
        String[] strings = companyInfo.split(",");
        company.setName(strings[0]);
        company.setAddress(strings[1]);
        company.setScale(Integer.parseInt(strings[2]));
        return company;
    }

    /**
     *
     * @return 返回FactoryBean创建的Bean类型
     */
    @Override
    public Class<?> getObjectType() {
        return null;
    }

    /**
     * 返回作⽤域是否单例
     * @return 单列作用域
     */
    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }

}
